package src.montecarlo;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

import src.utils.NormalFunction;
import src.utils.ProbabilityDistributionFunction;

public class Calibration {

	public static void main(String[] argvs) throws IOException{

		int sampleSize;
		double deltaMin;
		double deltaMax;
		double eps;
		double thermalizationTime;
		
		try {
			sampleSize = new Integer(argvs[0]);
			
			deltaMin = new Double(argvs[1]);
			deltaMax = new Double(argvs[2]);
			eps = new Double(argvs[3]);
			thermalizationTime = new Double(argvs[4]);	
		} catch (Exception e) {
			showUsage();
			return;
		}
		
		double mean = 0;
		double sigma = 1;
		ProbabilityDistributionFunction pdf = new NormalFunction(mean,sigma);
		
		FileOutputStream fos = new FileOutputStream(new File("rejections"+"-"+deltaMin*100+"-"+deltaMax*100));

		//fos.write("delta\trejection rate\ttime\n".getBytes());
		
		for(double delta = deltaMin; delta<deltaMax; delta+=eps){
					
			MetropolisMonteCarlo simulator = new MetropolisMonteCarlo(pdf,sampleSize,delta,thermalizationTime);
			
			long initTime = System.currentTimeMillis();
			//generate an output file with simulated values
			double rejectionRate = simulator.metropolisAlgorithm(pdf.name()+"-"+(delta*10000));
			long finishTime = System.currentTimeMillis();			
			
			fos.write((delta+","+rejectionRate+",").getBytes());
			
			//time: want to compute how inefficient it to increase delta
			fos.write((new Long(finishTime-initTime).toString()+"\n").getBytes());
			
							
			//generate an output file with correlations results
			//simulator.autoCorrelation("auto-"+(delta*10000));
			System.out.println(delta);
		}
		fos.flush();
		fos.close();
	}
	
	private static void showUsage() {
		
		System.out.println("Usage: ");	
		System.out.println("<sampleSize> <deltaMin> <deltaMax> <eps>\n");
		
		System.out.println("<sampleSize>: number of points on each run.");
		System.out.println("<deltaMin>: minimum value of delta.");
		System.out.println("<deltaMax>: maximum value of delta .");
		System.out.println("<eps>: step size between deltas.");
	}
}
